import animatepath from "./animatepath"
import lgxcom from '../common/common.js'
import utilfuncs from "../common/tools/utilfuncs";
var funcs = utilfuncs.UtilFuncs;

class GraphObjPath extends animatepath.AnimatePath {
    constructor(graph) {
        super();
        this.type = "obj";
        this.pathDesc = graph;
        this.path = graph.getPath();
        this.curentPt = null;
        this.stIdx = -1;
        this.transDir = new lgxcom.LGXLineSection();
    }
    getPath() {
        return this.path;
    }
    animate(speed) {
        if (!this.curentPt) {
            this.chgDir();
        }
        else {
            let dis = funcs.distancePoint(this.transDir.stPt, this.curentPt, 1);
            if (dis > this.transDir.lenth) {
                this.chgDir();
            }

            let x = this.curentPt.x + speed * this.transDir.vector.m;
            let y = this.curentPt.y + speed * this.transDir.vector.n;
            this.curentPt.x = x;
            this.curentPt.y = y;
            
        }
        return { pt: this.curentPt, dir: this.transDir };
    }

    chgDir() {
        let nxtStIdx = this.stIdx + 1;
        if (nxtStIdx >= this.path.length) {
            nxtStIdx = 0;
        }

        let nxtEndIdx = nxtStIdx + 1;
        if (nxtStIdx == this.path.length - 1) {
            nxtEndIdx = 0;
        }
        let p0 = this.path[nxtStIdx];
        let p1 = this.path[nxtEndIdx];
        this.curentPt = { x: p0.x, y: p0.y };
        this.transDir.setPt(p0, p1);
        this.stIdx = nxtStIdx;
    }
}

export default { GraphObjPath }